package com.dys.controller;

import com.dys.entity.Clazz;
import com.dys.service.ClassService;
import com.dys.service.impl.ClassServiceImpl;
import com.dys.util.PageUtil;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: 疾风
 * @ClassName: ClassServlet
 * @Description: 班级Servlet
 * @CreateDate: 2024/09/26
 * @Version: v1.0.0
 */
@WebServlet(urlPatterns = "/offlineSchool/classServlet.do")
public class ClassServlet extends HttpServlet {
    private final ClassService classService = new ClassServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-----进入保存的service方法中-----");
        String method = req.getParameter("method");
        if ("save".equals(method)) {
            save(req, resp);
        } else if ("findAllClasses".equals(method)) {
            listClass(req, resp);
        } else if ("findAllClassesNew".equals(method)) {
            listClassNew(req, resp);
        } else if ("deleteClassInfoByCid".equals(method)) {
            deleteClassInfoByCid(req, resp);
        } else if ("findOneClass".equals(method)) {
            findOneClass(req, resp);
        } else if ("updateOneClass".equals(method)) {
            updateOneClass(req, resp);
        } else if ("findClassCount".equals(method)) {
            findClassCountInfo(req, resp);
        } else if ("findDropDownToSelectAClass".equals(method)) {
            findDropDownToSelectAClass(req, resp);
        }
    }

    /**
     * 保存班级信息
     *
     * @param req  请求域对象
     * @param resp 响应对象
     * @throws ServletException Servlet异常
     * @throws IOException      IO异常
     */
    protected void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-----保存班级信息-----");
        // 获取前端传递的请求参数
        String cname = req.getParameter("cname");
        String cloc = req.getParameter("cloc");
        String cdesc = req.getParameter("cdesc");
        // 将参数封装为Clazz对象
        Clazz clazz = new Clazz(cname, cloc, cdesc);
        // 调用service层
        int i = classService.saveClass(clazz);
        System.out.println("保存状态：" + i);
        // 响应结果
        resp.getWriter().println(i);
    }

    /**
     * 查询所有班级信息
     *
     * @param req  请求域对象
     * @param resp 响应对象
     * @throws ServletException Servlet异常信息
     * @throws IOException      IO异常信息
     */
    protected void listClass(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-----查询所有班级信息-----");
        // 调用service层
        List<Clazz> allClass = classService.findAllClass();
        // 转为Json
        String classJson = new Gson().toJson(allClass);
        allClass.stream().forEach(System.out::println);
        // 响应-因为前台发送的是ajax请求，所以这里响应的是json字符串
        resp.getWriter().println(classJson);
    }

    /**
     * 查询所有班级信息
     *
     * @param req  请求域对象
     * @param resp 响应对象
     * @throws ServletException Servlet异常信息
     * @throws IOException      IO异常信息
     */
    protected void listClassNew(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-----分页查询所有班级信息-----");
        // 1.获取请求参数：当前页
        int page = Integer.parseInt(req.getParameter("page"));
        System.out.println("当前页：" + page);
        // 2.定义每页显示3条记录
        int size = 3;
        System.out.println("每页展示条数：" + size);

        // 3.根据page、PageUtil工具类获得更多数据
        PageUtil pu = new PageUtil();
        // 设置page
        pu.setPage(page);
        // 设置size
        pu.setSize(size);
        // 获取startIndex
        int startIndex = pu.getStartIndex();
        System.out.println("当前索引值：" + startIndex);

        // 查询班级总数
        List<Clazz> list = classService.selectClasses(startIndex, size);
        // 查询总条数
        int totalCount = classService.getCountClass();

        // 设置总条数
        pu.setTotalCount(totalCount);
        System.out.println("总条数：" + totalCount);
        // 设置总页数
        pu.setTotalPageCount();
        // 设置页码数组
        pu.setPageNums();
        // 获取页码数组
        int[] pageNums = pu.getPageNums();
        String s = "";
        for (int i = 0; i < pageNums.length; i++) {
            s += pageNums[i] + ",";
        }
        System.out.println(s);
        // 将数据放到分页工具类中
        pu.setList(list);
        // 转为Json
        String classJson = new Gson().toJson(pu);
        list.stream().forEach(System.out::println);
        // 响应-因为前台发送的是ajax请求，所以这里响应的是json字符串
        resp.getWriter().println(classJson);
    }

    /**
     * 根据cid删除班级信息
     *
     * @param req  请求域对象
     * @param resp 响应对象
     * @throws ServletException Servlet异常对象
     * @throws IOException      IO异常对象
     */
    protected void deleteClassInfoByCid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-----根据Cid删除班级信息-----");
        // 获取请求参数
        int cid = Integer.parseInt(req.getParameter("cid"));
        // 调用service层
        int rowCount = classService.deleteOneClass(cid);
        System.out.println("删除时受影响行数：" + rowCount);
        // 响应-因为前台发送的是ajax请求，所以这里响应的是json字符串
        resp.getWriter().println(rowCount);
    }

    /**
     * 修改班级信息
     *
     * @param req  请求域对象
     * @param resp 响应对象
     * @throws ServletException Servlet异常对象
     * @throws IOException      IO对象
     */
    protected void findOneClass(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-----查询单个班级信息-----");
        // 获取前端传递的请求参数
        int cid = Integer.parseInt(req.getParameter("cid"));
        // 调用service层
        Clazz oneClass = classService.findOneClass(cid);
        System.out.println("班级信息：" + oneClass);
        // 将班级信息放入请求域中
        req.setAttribute("oneClass", oneClass);
        // 请求转发到修改页面
        req.getRequestDispatcher("/updateClass.jsp").forward(req, resp);
    }

    /**
     * 修改班级信息
     *
     * @param req  请求域对象
     * @param resp 响应对象
     * @throws ServletException Servlet对象
     * @throws IOException      IO对象
     */
    protected void updateOneClass(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-----修改单个班级信息-----");
        // 获取前端传递的请求参数
        int cid = Integer.parseInt(req.getParameter("cid"));
        String cname = req.getParameter("cname");
        String cloc = req.getParameter("cloc");
        String cdesc = req.getParameter("cdesc");
        Clazz clazz = new Clazz(cid, cname, cloc, cdesc);
        // 调用service层
        int rowCount = classService.updateClass(clazz);
        System.out.println("修改班级信息受影响行数：" + rowCount);
        // 响应给前端
        resp.getWriter().println(rowCount);
    }

    /**
     * 展示班级信息
     *
     * @param req  请求对象
     * @param resp 响应对象
     * @throws ServletException Servlet对象
     * @throws IOException      IO对象
     */
    protected void findClassCountInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-----展示班级信息-----");
        // 调用service层
        List listClassInfo = classService.findClassCount();
        // 转为Json信息
        String classJson = new Gson().toJson(listClassInfo);
        System.out.println("班级信息：" + classJson);
        // 响应给前端
        resp.getWriter().println(classJson);
    }

    /**
     * 下拉选择所有班级信息
     *
     * @param req  请求域对象
     * @param resp 响应对象
     * @throws ServletException Servlet异常信息
     * @throws IOException      IO异常信息
     */
    protected void findDropDownToSelectAClass(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-----下拉选择所有班级信息-----");
        // 调用service层
        List<Clazz> allClass = classService.findAllClass();
        // 转为Json
        String classJson = new Gson().toJson(allClass);
        allClass.stream().forEach(System.out::println);
        // 响应-因为前台发送的是ajax请求，所以这里响应的是json字符串
        resp.getWriter().println(classJson);
    }
}
